<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> XSS-Cheat Sheet Tester</TITLE>
<META NAME="Author" CONTENT="huangwei.bupt@gmail.com">
<META NAME="Description" CONTENT="">
<style type="text/css">
.div-top {
	position:absolute;
	top: 4px;
	margin: 0px; 
	padding: 0px; 
	display: none; 
	background-color: #FFFF00; 
	color: #993300; 
	border: 1px solid #FFFFFF; 
	width:600px;
	text-align: center; 
}
.div-right {
 position:absolute;
 left:360;
 top:80;

}
.div-left {
 position:absolute;
 top:80;
 left:0;
 width:200px;
}
.s {
	color: #FF0000;
	}

.ns {
	color: #C0C0C0;
	}

</style>
</HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
/******************************************************
 * GLOBAL VAR Definitions start
 ******************************************************/
/*
 * This array of browser names drives all browser support display.
 */
var editBrowString = new Array('IE7.0','IE6.0','NS8.1-IE','NS8.1-G','FF2.0','O9.02');
var xssBrowserArray = new Array(	
	'Works in All','ALL'
	);
var dropdownLoadArray2 = new Array(
	xssBrowserArray,'browFilter'
	);
var textArray  = new Array();
var valueArray = new Array();
/******************************************************
 * GLOBAL VAR Definitions end
 ******************************************************/

/******************************************************
 * XML File Load and Parse start
 *******************************************************/
function loadXMLFile(selectIndex, url, topicsOnly, initLoad)
{
	var reqHttp_request = '';

    // Non-Windows XMLHttpRequest.
	if (window.XMLHttpRequest) {
		try {
			reqHttp_request = new XMLHttpRequest();
        } catch (e) {
			reqHttp_request = false;
			alert('Cannot create XMLHttpRequest\n' + e);
        }
		
		try {
			if (reqHttp_request) {
	
				reqHttp_request.onreadystatechange = function() {
					if (reqHttp_request.readyState == 4) {
						
						if (topicsOnly) {
							clearBox('xssType');
							if(initLoad){
								loadXmlTopics(reqHttp_request.responseXML, 'xssType', 'attack', 'name', 'name', true);
							}else {
								loadXmlTopics(reqHttp_request.responseXML, 'xssType', 'attack', 'name', 'name', false);
							}
							
						}
						else {
							loadDetails(selectIndex, reqHttp_request.responseXML, url);
						}
					}    
				}
				reqHttp_request.open("GET", url, true);
				reqHttp_request.send(null);
			} else {
				alert('XMLHttpRequest Retrieval Error');
			}	
		} catch(e) {
			alert('ERROR:\n' + e);
		}

    // Windows XMLHttpRequest.	
	} else if (window.ActiveXObject) {

		// Use XMLDOM instead of XMLHttpRequest
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.load(url);
	
		if (xmlDoc.readyState == 4) {
			
			if (topicsOnly) {
				clearBox('xssType');
				if(initLoad){
						loadXmlTopics(xmlDoc, 'xssType', 'attack', 'name', 'name', true);
				}
				else{
						loadXmlTopics(xmlDoc, 'xssType', 'attack', 'name', 'name', false);
				}
			}
			else{
				loadDetails(selectIndex, xmlDoc, url);
			}
		}
	}
}


//gets the content of the element
function getText(inElement) {
	
	if (typeof inElement.textContent != typeof undefined) {
		return inElement.textContent;
	} else if (typeof inElement.firstChild != typeof undefined) {
		return inElement.firstChild.nodeValue;
	} else {
		return null;
	}	
}

function loadDetails(pointer, xmlDoc, url)
{
	var xLoad = new Array();
	xLoad = xmlDoc.getElementsByTagName('attack');
	var xXmlLen = xLoad.length;

	var parentObj = document.MainDIV;
	for (var i=0;i < xXmlLen;i++) {
		if (xLoad[i].hasChildNodes()) {
			if (getText(xLoad[i].getElementsByTagName('name')[0]) == pointer) {
				document.getElementById('browser').innerHTML = 'Browser support:&nbsp;' + 
						browserDisplay(getText(xLoad[i].getElementsByTagName('browser')[0]));
				parentObj.xss_code.value  = getText(xLoad[i].getElementsByTagName('code')[0]);
				parentObj.xss_desc.value = getText(xLoad[i].getElementsByTagName('desc')[0]);
				return;	
			}
		}
	}

}

function loadXmlTopics(xmlDoc, selectID, xmlNode, textName, valName, showAll)
{
	var x = new Array();
	var xLen = 0;
	var label = ' ';
	var newOptgroup = '';
	var newOpt = '';
	var browNode = '';

	var sel = document.getElementById(selectID); 
	var attr = selectID.substring(0,2);

	if (xmlDoc) {
		x = xmlDoc.getElementsByTagName(xmlNode);
		xLen = x.length;

		var browVal = document.getElementById('browFilter').value;
		//alert(browVal);
		for (var i=0;i < xLen;i++) {
			if (x[i].hasChildNodes()) {

				if (typeof x[i].getElementsByTagName('browser')[0] != typeof undefined) {
					if (x[i].getElementsByTagName('browser').length > 0) {
						browNode = getText(x[i].getElementsByTagName('browser')[0]);
					} else {
						browNode = '';
					}	
				} else {
					browNode = '';
				}	

				//alert(browNode);
				//alert(browVal);

				if (showAll) {
					// Check that the new item is not part of the existing Optgroup
					if ((selectID != 'reqHistory') && (selectID != 'resHistory') && (selectID != 'lists') && (selectID != 'reqLists')) {
						if (label != getText(x[i].getElementsByTagName('label')[0])) {
							// Create new Optgroup and add to form.
							newOptgroup = document.createElement('optgroup');
							newOptgroup.label = getText(x[i].getElementsByTagName('label')[0]);
							newOptgroup.id = attr + i;
							sel.appendChild(newOptgroup);
							label = getText(x[i].getElementsByTagName('label')[0]);
						}
					}
					newOpt = document.createElement('option');
					newOpt.text  = getText(x[i].getElementsByTagName(textName)[0]);
					newOpt.value = getText(x[i].getElementsByTagName(valName)[0]);

					try { // IE browser
						if ((selectID != 'reqHistory') && (selectID != 'resHistory') && (selectID != 'lists') && (selectID != 'reqLists')) {
							sel.add(newOpt);
						} else {
							document.getElementById(selectID).add(newOpt);
						}
					} catch(ex) { // Firefox
						if ((selectID != 'reqHistory') && (selectID != 'resHistory') && (selectID != 'lists') && (selectID != 'reqLists')) {
							newOptgroup.appendChild(newOpt);
						} else {
							document.getElementById(selectID).appendChild(newOpt);
						}
					}
				}
				else if (browserType(browNode,browVal)) {
	
					// Check that the new item is not part of the existing Optgroup
					if (label != getText(x[i].getElementsByTagName('label')[0])) {
						// Create new Optgroup and add to form.
						newOptgroup = document.createElement('optgroup');
						newOptgroup.label = getText(x[i].getElementsByTagName('label')[0]);
						newOptgroup.id = attr + i;
						sel.appendChild(newOptgroup);
						label = getText(x[i].getElementsByTagName('label')[0]);
					}
	
					newOpt = document.createElement('option');
					newOpt.text  = getText(x[i].getElementsByTagName(textName)[0]);
					newOpt.value = getText(x[i].getElementsByTagName(valName)[0]);
					
					try { // IE browser
						if ((selectID != 'reqHistory') && (selectID != 'resHistory') && (selectID != 'lists') && (selectID != 'reqLists')) {
							sel.add(newOpt);
						} else {
							document.getElementById(selectID).add(newOpt);
						}
					} catch(ex) { // Firefox
						if ((selectID != 'reqHistory') && (selectID != 'resHistory') && (selectID != 'lists') && (selectID != 'reqLists')) {
							newOptgroup.appendChild(newOpt);
						} else {
							document.getElementById(selectID).appendChild(newOpt);
						}
					}
				}
			}
		}
	}

}

/*
 * Applies the Browser Filter to the attack list.
 */
function browserType(browserNode,browserFilter) {
	var browSplit = '';
	var browSplitLen = 0;
	
	var ebsLen = editBrowString.length;
	
	if (browserNode != '') {
		browSplit = browserNode.split(',');
		browSplitLen = browSplit.length;
	}
		
	switch (browserFilter) {
		case '':
			return true; 
			break;
		case 'ALL':
			for (var a=0;a<ebsLen;a++) {
				if (editBrowString[a] != browSplit[a]) {
					return false;
				}	
			}
			return true;
			break;
		default:
			for (var a=0;a<browSplitLen;a++) {
				if (browSplit[a] == browserFilter) {
					return true;
				}
			}
			return false;
	}
}
/******************************************************
 * XML File Load and Parse end
 *******************************************************/

/******************************************************
 * Write File to disk start
 *******************************************************/
function saveFile(fileUrl, content)
{
	var fileSave = false;

	fileSave = mozillaSaveFile(fileUrl, content);

	if (fileSave == false) {
		fileSave = ieSaveFile(fileUrl, content);
	}	

	if (fileSave) {
		return fileSave;
	} else {
		displayMessage('Error trying to save file');
		return false;
	}	
}

/*
 * Processing to save info using Firefox and other Mozilla browsers.
 */
function mozillaSaveFile(filePath, content) {
	var mozSave = '';
	var moz = '';

	if(window.Components) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			mozSave = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
			mozSave.initWithPath(filePath);
			
			if (!mozSave.exists()) {
				mozSave.create(0, 0664);
			}	
			
			moz = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
			moz.init(mozSave, 0x20 | 0x02, 00004, null);
			moz.write(content, content.toString().length);
			moz.flush();
			moz.close();
			
			return true;

		} catch (e) {
			alert("Error while attempting to save\n" + e.toString());
			return false;
		}
	} else {
		return false;
	}
}

/*
 * Processing to save info using Internet Explorer browser.
 */
function ieSaveFile(filePath, content) {
	var ieSave = '';
	var ie = '';
	
	try	{
		if (typeof window.ActiveXObject != typeof undefined) {
			ieSave = new ActiveXObject("Scripting.FileSystemObject");
		} else {
			return false;
		}	
	} catch (e) {
		alert("Error while attempting to save\n" + e.toString());
		return false;
	}

	if (ieSave) {
		ie = ieSave.OpenTextFile(filePath,2,-1,0);
		ie.Write(content);
		ie.Close();
		
		return true;
		
	} else {
		return false;
	}	
}

/******************************************************
 * Write File to disk end
 *******************************************************/

/******************************************************
 * Misc. Utilities start
 *******************************************************/
/*
 * Converts plaintext to Decimal Encoded text to make safe for display.
 */
function safeDisplay(source) {
	var safeResult = new stringBuffer();
	var strResult = '';
	var srcLength = 0;
	var safeReg = /[\w\s\.\,\-\;\!\@\#]/i;
	var lineBreak = /[\n]/g;
	var tabBreak = /[\t]/g;
	var otherSpace = /[\s]/g;

	// Cycle through each input character.
	if (source) {
		srcLength = source.length;

		for (var i=0; i<srcLength; i++) {

			if (safeReg.test(source.charAt(i))) {
				safeResult.sbAppend(source.charAt(i));
			} else {
				safeResult.sbAppend('&#0' + source.charCodeAt(i) + ';');
			}
		}		
	}
		
	strResult = safeResult.sbToString();
	safeResult.length = 0;
	
	strResult = strResult.replace(lineBreak,'<BR>');
	strResult = strResult.replace(tabBreak,'&nbsp;&nbsp;&nbsp;&nbsp;');
	strResult = strResult.replace(otherSpace,'&nbsp;');

	return strResult;
} 

/*
 * Create String Buffer for faster string concatenation.
 */
function stringBuffer() { 
   this.buffer = []; 
 } 

 stringBuffer.prototype.sbAppend = function sbAppend(string) { 
   this.buffer.push(string); 
   return this; 
 }; 

 stringBuffer.prototype.sbToString = function sbToString() { 
   return this.buffer.join(""); 
 }; 

/*
 * Displays informational and error messages.
 */
function displayMessage(messageText) {
	var displayMess = '';
	var storedMess = '';	

	displayMess = document.getElementById('message');
	displayMess.style.display = "inline";
	displayMess.style.overflow = "hidden";

	if (messageText != '') {
		storedMess = safeDisplay(messageText);
	}
	
	displayMess.innerHTML = '<p>'+ storedMess + '</p>';

	setTimeout("hideStore('message')",3000);
}

function hideStore(divName) {
	document.getElementById(divName).style.display = "none";
}


function testScriptRun(obj){
	displayMessage(obj.value);
	executeScript(obj.value);
}

/*
 * Opens selected text in a new window and tries to execute it.
 */
function executeScript(testScript) {
	
	var script = testScript;
	var new_window = ''; 
	
	new_window = window.open('scriptTest.html','ScriptTest','scrollbars=yes,resizable=yes,width=400,height=300');

	new_window.document.writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">' +
		'<html><head><title>Test Results</title></head><body>' +
		'<form><input type="button" value="Close Window" onClick="self.close();"</form><HR>Source Rendering:<BR><BR>' +
		safeDisplay(script.valueOf()) + '<BR><BR><HR>Script execution:<BR><BR>' + script.valueOf() + '<HR></body></html>');
	displayMessage(script.valueOf());
	new_window.document.close();	
}
/******************************************************
 * Misc. Utilities start
 *******************************************************/


 /******************************************************
 * GUI components init start
 *******************************************************/

/*
 * Builds Supported Browser string.
 */
function browserDisplay(browList) {
	var browSplit = '';
	var browOut = '';
	var match = false;
	
	if (browList != '') {
		browSplit = browList.split(',');
	}

	if (browSplit.length > 0) {
		for (var a=0;a<editBrowString.length;a++) {
			for (var b=0;b<browSplit.length;b++) {
				if (editBrowString[a] == browSplit[b]) {
					browOut += '[<span class="s">' + browSplit[b] + '</span>]';
					match = true;
				} 
			}
			
			if (!match) {
				browOut += '[<span class="ns">' + editBrowString[a] + '</span>]';
			}
			match = false;
		}
	}
	return browOut;
}

/*
 * Adds the options for the small select boxes.
 */
function addOptions(inID) {
	var newOpt = '';
	var elementSel = document.getElementById(inID);
	
	for (k in textArray) {

		if (typeof textArray[k] != typeof undefined) {
			// Create the new option.
			newOpt = document.createElement('option');

			// Load the text
			newOpt.text = textArray[k];
	
			// Load the value
			if (valueArray[k] == null) {
				newOpt.value = k;
			} else {
				newOpt.value = valueArray[k];
			}
			
			// Add the new option to the select box.
			try {
				elementSel.add(newOpt, null); // Standards compliant (non-IE)
			} catch(ex) {
				elementSel.add(newOpt); // IE only
			}
		}
	}
}

function loadDropDowns() {
	loadXssBrowserArray();
	loadDropDowns2();
}

/*
 * Loads the browser filter array and creates the browser checkboxes.
 */
function loadXssBrowserArray() {
	
	for (var e=0;e<editBrowString.length;e++) {
		xssBrowserArray.push('Works in ' + editBrowString[e]);
		xssBrowserArray.push(editBrowString[e]);
	}
}

/*
 * Load drop-downs type2.
 */
function loadDropDowns2() {

	var ddlaLen2 = dropdownLoadArray2.length;
	
	for (var b=0; b<ddlaLen2; b++) {
		textValueDifferent(dropdownLoadArray2[b],dropdownLoadArray2[b+1]);
		b+=1;
	}	
}

/*
 * Adds the options for the small select boxes that have different Text and Value values.
 */
function textValueDifferent(arrayName,id) {

	var arrayLen = arrayName.length;
	
	for (var j=0; j<arrayLen; j++) {
		textArray[j]  = arrayName[j];
		valueArray[j] = arrayName[j+1];
		j+=1;
	}
	
	addOptions(id);		
	clearDDArrays();
}

/*
 * Adds the options for the small select boxes.
 */
function addOptions(inID) {
	var newOpt = '';
	var elementSel = document.getElementById(inID);
	
	for (k in textArray) {

		if (typeof textArray[k] != typeof undefined) {
			// Create the new option.
			newOpt = document.createElement('option');

			// Load the text
			newOpt.text = textArray[k];
	
			// Load the value
			if (valueArray[k] == null) {
				newOpt.value = k;
			} else {
				newOpt.value = valueArray[k];
			}
			
			// Add the new option to the select box.
			try {
				elementSel.add(newOpt, null); // Standards compliant (non-IE)
			} catch(ex) {
				elementSel.add(newOpt); // IE only
			}
		}
	}
}

/*
 * Clears out the Text and Value arrays.
 */
function clearDDArrays() {  
	textArray  = new Array();
	valueArray = new Array();
}

/*
 * Clears out the contents of a select box.
 */
function clearBox(boxID) {
	var selID = '';
	
	if (boxID != '') {
		selID = document.getElementById(boxID); 
	}

	if (selID) {
		while (selID.firstChild) { 
			selID.removeChild(selID.firstChild); 
		}
	}
}

 /******************************************************
 * GUI components init end
 *******************************************************/
//-->
</SCRIPT>

<BODY onload="loadXMLFile('', 'xssAttacks.xml', true, true);loadDropDowns();">
	<form name="MainDIV" style="position:relative; top:20; left:20;">
		<div id="message" class="div-top"></div>
		<div class="div-left">
			<select id="xssType" size="29" style="max-width:250px" onClick="loadXMLFile(this.value,'xssAttacks.xml', false, false);">
			</select>
		</div>
		<div class="div-right">
			<DIV id="browser">Browser support:&nbsp;</DIV>
			Code:<br/>
			<textarea name="xss_code" cols=80 rows=5></textarea><br/>
			Description:<br/>
			<textarea name="xss_desc" cols=80 rows=10></textarea><br/>
			Filter:&nbsp;
			<select id="browFilter" size="1" onChange="loadXMLFile('','xssAttacks.xml', true, false);">
				<option value="">Show All Attacks</option>
			</select><br/>
			<input type=button value="TestExecuteCode" onclick="testScriptRun(document.MainDIV.xss_code);"/>
		</div>
	</form>

</BODY>
</HTML>
